<?php

class EnquaryController extends GxController {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'view'),
                'users' => array('@'),
            ),
            array('allow',
                'actions' => array('minicreate', 'create', 'update', 'admin', 'delete', 'assign'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Enquary'),
        ));
    }

    public function actionCreate() {
        $model = new Enquary;

        $this->performAjaxValidation($model, 'enquary-form');

        if (isset($_POST['Enquary'])) {
            $model->setAttributes($_POST['Enquary']);
            if (isset($_POST['Enquary']['enquiry_type']) && is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 1 && $_POST['Enquary']['enquiry_type'][0] == 'R') {
                $model->enquiry_type = 'R';
            } elseif (isset($_POST['Enquary']['enquiry_type']) && (is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 1 && $_POST['Enquary']['enquiry_type'][0] == 'O')) {
                $model->enquiry_type = 'O';
            } elseif ((isset($_POST['Enquary']['enquiry_type']) && is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 2)) {
                $model->enquiry_type = 'B';
            }
            $model->users_id = Yii::app()->user->id;
//            $model->person_image = CUploadedFile::getInstance($model, 'person_image');
//            $model->poi_image = CUploadedFile::getInstance($model, 'poi_image');
//            $model->poa_image = CUploadedFile::getInstance($model, 'poa_image');
            // $model->signature = CUploadedFile::getInstance($model, 'signature');
            //  $folder = YiiBase::getPathOfAlias('webroot') . '/upload/'; // folder for uploaded files
//                if (!empty($model->person_image)) {
//                    $model->person_image = CUploadedFile::getInstance($model, 'person_image');
//                    $model->person_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->person_image);
//                    $pesonImage = guid() . '.jpg';
//                    rename("$folder" . "$model->person_image", $folder . $pesonImage);
//                    $model->person_image = $pesonImage;
//                }
//
//
//                if (!empty($model->poi_image)) {
//                    $model->poi_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->poi_image);
//                    $poiImage = guid() . '.jpg';
//                    rename($folder . $model->poi_image, $folder . $poiImage);
//                    $model->poi_image = $poiImage;
//                }
//
//                if (!empty($model->poa_image)) {
//                    $model->poa_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->poa_image);
//                    $poaImage = guid() . '.jpg';
//                    rename("$folder" . " $model->poa_image", $folder . $poaImage);
//                    $model->poa_image = $poaImage;
//                }
//
//                if (!empty($model->signature)) {
//                    $model->signature->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->signature);
//                    $sign = guid() . '.jpg';
//                    rename($folder . $model->signature, $folder . $sign);
//                    $model->signature = $sign;
//                }
            if ($model->save()) {

                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Enquary');


        $this->performAjaxValidation($model, 'enquary-form');

        if (isset($_POST['Enquary'])) {

            $model->setAttributes($_POST['Enquary']);
            if (isset($_POST['Enquary']['enquiry_type']) && is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 1 && $_POST['Enquary']['enquiry_type'][0] == 'R') {
                $model->enquiry_type = 'R';
            } elseif (isset($_POST['Enquary']['enquiry_type']) && (is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 1 && $_POST['Enquary']['enquiry_type'][0] == 'O')) {
                $model->enquiry_type = 'O';
            } elseif ((isset($_POST['Enquary']['enquiry_type']) && is_array($_POST['Enquary']['enquiry_type']) && count($_POST['Enquary']['enquiry_type']) == 2)) {
                $model->enquiry_type = 'B';
            }

//            $model->person_image = CUploadedFile::getInstance($model, 'person_image');
//            $model->poi_image = CUploadedFile::getInstance($model, 'poi_image');
//            $model->poa_image = CUploadedFile::getInstance($model, 'poa_image');
//            $model->signature = CUploadedFile::getInstance($model, 'signature');
//$folder = YiiBase::getPathOfAlias('webroot') . '/upload/'; // folder for uploaded files
//                if (!empty($model->person_image)) {
//                    $model->person_image = CUploadedFile::getInstance($model, 'person_image');
//                    $model->person_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->person_image);
//                    $pesonImage = guid() . '.jpg';
//                    rename("$folder" . "$model->person_image", $folder . $pesonImage);
//                    $model->person_image = $pesonImage;
//                }
//
//
//                if (!empty($model->poi_image)) {
//                    $model->poi_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->poi_image);
//                    $poiImage = guid() . '.jpg';
//                    rename($folder . $model->poi_image, $folder . $poiImage);
//                    $model->poi_image = $poiImage;
//                }
//
//                if (!empty($model->poa_image)) {
//                    $model->poa_image->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->poa_image);
//                    $poaImage = guid() . '.jpg';
//                    rename("$folder" . " $model->poa_image", $folder . $poaImage);
//                    $model->poa_image = $poaImage;
//                }
//
//                if (!empty($model->signature)) {
//                    $model->signature->saveAs(YiiBase::getPathOfAlias('webroot') . '/upload/' . $model->signature);
//                    $sign = guid() . '.jpg';
//                    rename($folder . $model->signature, $folder . $sign);
//                    $model->signature = $sign;
//                }

            if ($model->save()) {

                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Enquary')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        }
        else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('Enquary');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new Enquary('search');
        $model->unsetAttributes();

        if (isset($_GET['Enquary']))
            $model->setAttributes($_GET['Enquary']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionAssign($id) {

        $user = Users::model()->with(array(
                    'userHasRoles' => array(
                        // we don't want to select userhasroles field
                        'select' => false,
                        // but want to get only users with role id 1
                        'joinType' => 'INNER JOIN',
                        'condition' => 'userHasRoles.roles_id=' . Roles::APPUSER,
                    ),
                ))->findAll();
        ;

        $model = $this->loadModel($id, 'Enquary');
        $selected = array();
        $aEnquary = AssignedEnquary::model()->findAll('enquary_id=:enquary_id ', array(':enquary_id' => $model->id));
        if (!empty($aEnquary) && is_array($aEnquary))
            foreach ($aEnquary as $key1 => $eId) {


                $selected[] = $eId->users_id;
            }

        if (isset($_POST['appuserSave'])) {
            foreach ($_POST['AppUsers'] as $key => $value) {
                if (!empty($value)) {
                    $assignedEnquary = new AssignedEnquary();
                    $assignedEnquary->users_id = $value;
                    $assignedEnquary->enquary_id = $model->id;
                    $assignedEnquary->save(false);
                    $this->redirect(array('enquary/admin'));
                }
            }
        }

        $this->render('assign', array(
            'model' => $model,
            'user' => $user,
            'selected' => $selected,
        ));
    }

}